package com.ebelter.myehc.ui.fragments.modifys;

import android.app.Dialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.bases.fragment.BaseFragment;
import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.beans.ScaleUser;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.BaseResponse;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.WeightSelectDialog;
import com.ebelter.btcomlib.views.dialogs.TextTipDialog;
import com.ebelter.myehc.R;
import com.ebelter.myehc.models.http.request.EhcNetUtils;
import com.ebelter.myehc.ui.activitys.MainActivity;
import com.ebelter.myehc.ui.view.CombinView6;

import org.greenrobot.eventbus.EventBus;


/**
 * 血糖
 */

public class ModifyWeight_Fragment extends BaseFragment implements View.OnClickListener {


    public static final String TAG = "ModifyWeight_Fragment";

    private TextView m_title_tv;
    private TextView m_righttitle_tv;
    private ImageView top_left_iv;
    
    private CombinView6 height_cb6;




    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_modify_height;
    }

    @Override
    protected void initView() {
        super.initView();

        FV();
        m_title_tv.setText(R.string.tizhong);
        m_righttitle_tv.setText(R.string.done);

        weightValue= UserSpUtil.readFloat(Constants.IUser.WEIGHT);
        height_cb6.setTitle1(StringUtils.getResStr(R.string.tizhong));
        height_cb6.setTitle2(weightValue+" Kg");
    }
    @Override
    protected void setListener() {
        super.setListener();

        m_righttitle_tv.setOnClickListener(this);
        top_left_iv.setOnClickListener(this);
        height_cb6.setOnClickListener(this);

    }




    private void FV() {
        m_title_tv=mRootView.findViewById(R.id.m_title_tv);
        m_righttitle_tv=mRootView.findViewById(R.id.m_righttitle_tv);
        top_left_iv=mRootView.findViewById(R.id.top_left_iv);
        height_cb6=mRootView.findViewById(R.id.height_cb6);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.m_righttitle_tv:
                complete ();
                break;
            case R.id.top_left_iv:
                back();
                break;
            case R.id.height_cb6:
                height_cb6_Click();
                break;
        }
    }

    private float weightValue;
    private float weightValue2;
    private void height_cb6_Click() {

        if (getContext()==null) return;

        height_cb6.setImage1(R.drawable.ic_shangla);


        WeightSelectDialog weightSelectDialog=new WeightSelectDialog(getActivity());
        weightSelectDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                height_cb6.setImage1(R.drawable.ic_xiala);
            }
        });
        weightValue =UserSpUtil.readFloat(Constants.IUser.WEIGHT,50);
        weightValue2=weightValue;
        weightSelectDialog.show(weightValue);
        weightSelectDialog.setValueChange(new WeightSelectDialog.ValueChange() {
            @Override
            public void change(float value) {
                LogUtils.i(TAG,"--weightSelectDialog--setValueChange--value = "+value);
                weightValue2 = value;
            }
        });

        weightSelectDialog.setBackgroundColor(getResources().getColor(R.color.redTheme));
        weightSelectDialog.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {}//内部已经处理了

            @Override
            public void ok(Dialog dialogView) {
                weightValue=weightValue2;
                if (height_cb6!=null){height_cb6.setTitle2(weightValue+" Kg");}

            }
        });


    }

    //更新身高信息
    private void updatePersonWeight(final float weight) {

        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }




        String authKey = MainActivity.getAuthKey();
        EhcNetUtils.getInstance().updatePersonWeight(this, authKey, weight, new HttpResponse<BaseResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, BaseResponse baseResponse, String s) {
                LogUtils.i(TAG,s);
                if (isRequestSuccess&&baseResponse.resultCode==1){
                    //说明修改成功
                    ToastUtil.show(R.string.Modify_Success);

                    UserSpUtil.writeFloat(Constants.IUser.WEIGHT,weight);
                    ScaleUser.getUser().setWeight(weight);

                    if (height_cb6!=null)height_cb6.setTitle2(weight+" Kg");

                    // TODO: 2018/8/28   如果此时连接的是体脂秤 需要下发新的用户信息给秤
                    CommonEventBus commonEventBus = new CommonEventBus(TAG, MainActivity.TAG,0,"设置了身高-需要重新下发用户信息");
                    EventBus.getDefault().post(commonEventBus);

                }else {
                    //说明修改失败
                    ToastUtil.show(R.string.Modify_Failed);
                }

            }
        });

    }

    private void back() {   if (mActivity!=null)mActivity.finish();
    }

    private void complete() {

        updatePersonWeight(weightValue);
    }
}
